import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xiaofanshu_flutter/controller/attention_message_controller.dart';
import 'package:xiaofanshu_flutter/static/custom_color.dart';
import 'package:xiaofanshu_flutter/utils/date_show_util.dart';

class AttentionMessage extends StatefulWidget {
  const AttentionMessage({super.key});

  @override
  State<AttentionMessage> createState() => _AttentionMessageState();
}

class _AttentionMessageState extends State<AttentionMessage> {
  AttentionMessageController controller = Get.find();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        centerTitle: true,
        leading: IconButton(
          icon: const Icon(
            Icons.arrow_back_ios,
            color: Colors.black,
          ),
          onPressed: () {
            Get.back();
          },
        ),
        title: const Text(
          '新增关注',
          style: TextStyle(fontSize: 16),
        ),
        elevation: 1,
        shape: const Border(
          bottom: BorderSide(
            color: Color(0xffe5e5e5),
            width: 0.5,
          ),
        ),
        actions: [
          IconButton(
            icon: const Icon(Icons.delete_outline, color: Colors.black),
            onPressed: () {
              showDialog(
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    title: const Text('清空关注消息'),
                    content: const Text('确定要清空所有关注消息吗？'),
                    actions: [
                      TextButton(
                        child: const Text('取消'),
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                      ),
                      TextButton(
                        child: const Text('确定'),
                        onPressed: () {
                          controller.clearAll();
                          Navigator.of(context).pop();
                        },
                      ),
                    ],
                  );
                },
              );
            },
          ),
        ],
      ),
      body: Obx(
        () => RefreshIndicator(
          onRefresh: () async {
            controller.onRefresh();
          },
          color: CustomColor.primaryColor,
          child: controller.attentionMessageList.isEmpty
              ? const Center(
                  child: Text(
                    '暂无关注消息',
                    style: TextStyle(
                      fontSize: 14,
                      color: Colors.grey,
                    ),
                  ),
                )
              : ListView.builder(
                  controller: controller.scrollController,
                  physics: const AlwaysScrollableScrollPhysics(),
                  itemCount: controller.attentionMessageList.length,
                  itemBuilder: (BuildContext context, int index) {
                    return _messageItem(index);
                  },
                ),
        ),
      ),
    );
  }

  Widget _messageItem(int index) {
    var message = controller.attentionMessageList[index];
    return Container(
      decoration: const BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: Color(0xffe5e5e5),
            width: 0.5,
          ),
        ),
      ),
      margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
      child: GestureDetector(
        onTap: () {
          Get.toNamed('/other/mine', arguments: message['user_id']);
        },
        behavior: HitTestBehavior.opaque,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            GestureDetector(
              onTap: () {
                Get.toNamed('/other/mine', arguments: message['user_id']);
              },
              child: Container(
                margin: const EdgeInsets.only(top: 10, bottom: 10),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(25),
                  child: Image.network(
                    message['avatar_url'],
                    width: 40,
                    height: 40,
                  ),
                ),
              ),
            ),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  GestureDetector(
                    onTap: () {
                      Get.toNamed('/other/mine', arguments: message['user_id']);
                    },
                    child: Container(
                      margin: const EdgeInsets.only(left: 10),
                      child: Text(
                        message['user_name']??'',
                        style: const TextStyle(
                          fontSize: 14,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    margin: const EdgeInsets.only(left: 10),
                    child: Text(
                      '${message['content']}  ${DateShowUtil.showDateWithTime(message['time']??0)}',
                      style: const TextStyle(
                        fontSize: 13,
                        color: Colors.grey,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            IconButton(
              icon: const Icon(Icons.close, size: 16, color: Colors.grey),
              onPressed: () {
                controller.deleteMessage(message['id']);
              },
            ),
          ],
        ),
      ),
    );
  }
}